package com.xbb.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xbb.entity.*;
import com.xbb.service.IQingjiaService;
import com.xbb.util.MD5;
import com.xbb.util.Result;
import com.xbb.util.Result1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Mr xie
 * @since 2020-06-15
 */
@RestController
@RequestMapping("/qingjia")
public class QingjiaController {
    @Autowired
    IQingjiaService iQingjiaService;

    @PostMapping("/addQingjia")
    public Object addQingjia(Qingjia qingjia, Integer userId, String userName) {
        qingjia.setUserId(userId);
        qingjia.setUserName(userName);
        boolean save = iQingjiaService.save(qingjia);
        if (save) {
            return new Result(0, "请假成功");
        } else {
            return new Result(1, "请假失败");
        }
    }

    @PostMapping("/getQingjiaList")
    public Object getQingjiaList(Qingjia qingjia,Integer id,
                              @RequestParam(required = false, defaultValue = "1") Integer page,
                              @RequestParam(required = false, defaultValue = "10") Integer limit) {
        QueryWrapper<Qingjia> queryWrapper = new QueryWrapper<Qingjia>();
        if (!StringUtils.isEmpty(qingjia.getUserName())) {
            queryWrapper.like("user_name", qingjia.getUserName());
        }
        if (!StringUtils.isEmpty(qingjia.getStartTime())) {
            queryWrapper.gt("start_time",qingjia.getStartTime());
        }
        if (!StringUtils.isEmpty(qingjia.getEndTime())) {
            queryWrapper.lt("start_time", qingjia.getEndTime());
        }
        if (!StringUtils.isEmpty(qingjia.getIsPizhun())) {
            queryWrapper.eq("is_pizhun", qingjia.getIsPizhun());
        }
        Page<Qingjia> page1 = new  Page<Qingjia>(page,limit);
        IPage<Qingjia> iPage = iQingjiaService.page(page1, queryWrapper);
        List<Qingjia> data = iPage.getRecords();
        long total = iPage.getTotal();
        return new Result(0,"请假列表",total,data);
    }

    @PostMapping("/updateQingjia")
    public Object updateQingjia(@RequestParam Integer id){
        Qingjia qingjia = new Qingjia();
        qingjia.setId(id);
        qingjia.setIsOpen("已打开");
        qingjia.setIsPizhun("已批准");
        boolean update = iQingjiaService.updateById(qingjia);
        if(update) {
            return new Result(0,"批假成功");
        }else {
            return new Result(1,"批假失败");
        }
    }

    @RequestMapping("/getMyQingJiaList")
    public Object getMyQingJiaList(Qingjia qingjia, Integer userId) {
        qingjia.setId(userId);
        List<Qingjia> list = iQingjiaService.getMyQingJiaList(qingjia);
        qingjia.setPage(null);
        qingjia.setLimit(null);
        List<Qingjia> list2 = iQingjiaService.getMyQingJiaList(qingjia);
        return new Result1(0, "我的请假列表", list2.size(), list);
    }


}
